/**
 * Advanced Software Engineering (Monica Farrow)
 * TextWriter class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depotAdmin;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * TextWriter class for creating event log entries
 * and final report
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class TextWriter {

	// holds instance of TextWriter (Singleton pattern)
	private static TextWriter instance; 
	// holds 1st part of final report to be received
	private static String partFinalReport = ""; 


	/**
	 * returns an instance of EventLog
	 * using synchronization due to threaded environment
	 * and 2-phase test to ensure is only created when actually needed
	 * @return the instance of EventLog
	 */
	public static TextWriter getInstance() {

		if (instance==null) {
			synchronized (TextWriter.class) {
				if (instance==null) {
					instance = new TextWriter();
				}
			}
		} 
		return instance;
	}

	/**
	 * obtains correctly formatted date for eventLog entry
	 * @return  the formatted date
	 */
	public  String getLogTime() {

		String eventLogDate = "";

		//get current date/time with Date()
		Date date = new Date();

		// format date for eventLog file entries
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy [HH:mm:ss]");

		// produce entry for queueLog file
		eventLogDate = dateFormat.format(date);

		return eventLogDate;
	}


	/**
	 * writes supplied text to file
	 * @param filename the name of the file to be written to
	 * @param report the text to be written to the file
	 * @param mode   information about type of text
	 */
	public  void writeToFile(String filename, String report, String mode) {
		FileWriter fw = null;
		String type ="";

		try {
			// get data from mode
			String modeArgs [] = mode.split(" ");

			// check to see if is final report
			if (modeArgs[0].equalsIgnoreCase("report")) {

				// if only one part has been received, put in partFinalReport
				if(getPartFinalReport().equalsIgnoreCase("")){
					setPartFinalReport(report);
					return;
				}
				// if both parts have been received
				else {
					fw = new FileWriter(filename);	
					// see which part has just been received 
					// and put into correct order for printing
					if(modeArgs[1].equalsIgnoreCase("1")) {
						report += getPartFinalReport();
					}
					else {
						report = getPartFinalReport() + report;						
					}
				}

			}
			// if it is an eventLog entry
			else if (modeArgs[0].equalsIgnoreCase("append")) {
				fw = new FileWriter(filename, true);
				if (report == null || report.equalsIgnoreCase("")) {  // catch empty strings
					type = "<ERR> ";
					report = "eventLog creation error";
				}
				else {
					type = report.substring(0, 6);
					report = report.substring(6);
				}
				report = type + getLogTime() + " " + report;

			}

			fw.write(report);
			fw.close();
		}
		//message and stop if file not found
		catch (NullPointerException ne){
			System.out.println(report);
			ne.printStackTrace();
			System.exit(0);
		}
		//message and stop if file not found
		catch (FileNotFoundException fnf){
			System.out.println(filename + " not found");
			System.exit(0);
		}
		//stack trace here because we don't expect to come here
		catch (IOException ioe){
			ioe.printStackTrace();
			System.exit(1);
		}
	}

	/**
	 * gets value of partFinalReport
	 * @return String partFinalReport 
	 */
	public static String getPartFinalReport() {
		return partFinalReport;
	}

	/**
	 * sets value of partFinalReport to value contained in parameter
	 * @param pfr   String value 
	 */
	public static void setPartFinalReport(String pfr) {
		TextWriter.partFinalReport = pfr;
	}


}